# 关闭swap
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# 安装所需软件
sudo apt install -y --no-install-recommends conntrack ipvsadm ntp ipset jq iptables curl sysstat libseccomp2 ipvsadm

# 更新时间
sudo ntpdate time1.aliyun.com

# 加载内核模块
sudo modprobe ip_vs_rr
sudo modprobe br_netfilter

# 优化内核参数
sudo tee /etc/sysctl.d/kubernetes.conf <<-'EOF'
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 # 禁止使用 swap 空间，只有当系统 OOM 时才允许使用它
vm.overcommit_memory=1 # 不检查物理内存是否够用
vm.panic_on_oom=0 # 开启 OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
sudo sysctl -p /etc/sysctl.d/kubernetes.conf

version=`kubelet --version |  cut -d ' ' -f 2`
images=`kubeadm --kubernetes-version=$version config images list`

for imageName in ${images[@]} ; do
    image=${imageName/k8s.gcr.io\//}
    docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/$image
    docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/$image k8s.gcr.io/$image
done

sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version $version

sudo export KUBECONFIG=/etc/kubernetes/admin.conf
sudo iptables -P FORWARD ACCEPT

# 安装网络插件
# 手动拉取flannel的docker镜像
docker pull easzlab/flannel:v0.11.0-amd64
# 修改镜像名称
docker tag easzlab/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

